package liurong.driver.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * 运用CountDownLatch模拟并发访问
 * 
 * @author liurong
 *
 */
public class CountDownLatchTest1 {

	public static void main(String[] args) {
		CountDownLatch countDownLatch = new CountDownLatch(1);
		for (int i = 0; i < 100; i++) {
			Thread thread = new Thread(new AThread(countDownLatch) {
				@Override
				public void realRun() {
					System.out.println("线程执行");
				}

			});
			thread.start();
		}
		System.out.println("finish");
		countDownLatch.countDown();

	}

	private static abstract class AThread implements Runnable {
		private CountDownLatch countDownLatch;

		public AThread(CountDownLatch countDownLatch) {
			this.countDownLatch = countDownLatch;
		}

		public void run() {
			// 等待
			try {
				countDownLatch.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.realRun();
		}

		// 各个实现执行的方法不同
		public abstract void realRun();
	}
}
